# Copyright (C) 2009 Paul Rudin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import time #testing only

import OpenGL.GL as gl
import OpenGL.GLU as glu
import OpenGL.GLUT as glut

from OpenGL.arrays import numpymodule
numpymodule.NumpyHandler.ERROR_ON_COPY = True

import model_utils

def render(model, width, height, buffers):
    # change these at your peril - the projections in each dimension
    # need to be compatible with the way we mask things out.
    views = [[0.5, 0.5, 0, 0.5, 0.5, 0.5, 1, 0, 0],
             [0.5, 0, 0.5, 0.5, 0.5, 0.5, 1, 0, 0],
             [0, 0.5, 0.5, 0.5, 0.5, 0.5, 0, 1, 0]]
    del(buffers[:])
    for v in views:
        for depth_clear, depth_test in (1, gl.GL_LESS), (0, gl.GL_GREATER):
            gl.glClearDepth(depth_clear)
            gl.glDepthFunc(depth_test)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            gl.glMatrixMode(gl.GL_MODELVIEW)
            gl.glLoadIdentity()
            glu.gluLookAt(*v)
            model_utils.draw(model)
            gl.glFinish()
            pixels = gl.glReadPixelsf(0, 0, width, height,
                                      gl.GL_DEPTH_COMPONENT)
            buffers.append(pixels)
            #time.sleep(1)
    glut.glutLeaveMainLoop()

def reshape(height, width):
    gl.glViewport(0, 0, height, width)
    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity()
    gl.glOrtho(-0.5, 0.5, -0.5, 0.5, 0, 1)
    gl.glMatrixMode(gl.GL_MODELVIEW)
    
def initialize(width, height):
    glut.glutInit([])
    glut.glutSetOption(glut.GLUT_ACTION_ON_WINDOW_CLOSE,
                       glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS)
    glut.glutInitDisplayMode(glut.GLUT_SINGLE | glut.GLUT_RGB | glut.GLUT_DEPTH)
    glut.glutInitWindowSize(width, height)
    glut.glutCreateWindow("Voxy")
    gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glEnable(gl.GL_DEPTH_TEST)

def compute_voxels(znear, zfar, ynear, yfar, xnear, xfar):
    return  ( (xnear < xfar) &
              (ynear < yfar)[:,None] &
              (znear < zfar)[:, :, None])

def check_done(buffers):
    if len(buffers) == 6:
        glut.glutLeaveMainLoop()

def voxelize(model, resolution):
    width = height = resolution
    initialize(width, height)
    buffers=[]
    glut.glutDisplayFunc(lambda  : render(model, width, height, buffers))
    glut.glutReshapeFunc(lambda x, y: reshape(width, height))
    glut.glutIdleFunc(lambda : check_done(buffers))
    glut.glutMainLoop()
    return compute_voxels(*buffers)

